/* common.h : dgnsc common
 * Copyright (C) 2009-2010 drangon <drangon.zhou@gmail.com>
 * 2009-05
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef INCLUDED_DGNSC_COMMON_H
#define INCLUDED_DGNSC_COMMON_H

#include <stddef.h> // NULL
#include <stdint.h>

#include <assert.h>

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus


// <1> lib init/fini
const char * dgn_version();
int dgn_init( const char * log_file_name );
int dgn_fini();


// <2> assert and log
#define ASSERT(x) assert(x)

#ifdef _WIN32
#define DGN_LOG_LINE_END "\r\n"
#else
#define DGN_LOG_LINE_END "\n"
#endif

#define PR_DEBUG PR_LOG
#define PR_LOG( fmt, ... ) dgn_print_log( __FILE__, __LINE__, fmt DGN_LOG_LINE_END, ##__VA_ARGS__ )

void dgn_print_log( const char * file, int line, const char * fmt, ... );


// <3> mutex
typedef void * dgn_mutex_t;

dgn_mutex_t dgn_mutex_init();
void dgn_mutex_fini( dgn_mutex_t mutex );

int dgn_mutex_lock( dgn_mutex_t mutex );
int dgn_mutex_unlock( dgn_mutex_t mutex );


// <4> time
// (MSec) Relative time, from system start maybe higher resolution 
// not affect by time change, wrap around every 49.7 day
uint32_t dgn_time_tick();

// (USec) Epoch time ( from 1970-1-1 )
int64_t dgn_time_now();
int64_t dgn_time_from( int year, int month, int day, int hour, int min, int sec, int usec );
void dgn_time_to( int64_t ts, int * year, int * month, int * day, int * hour, int * min, int * sec, int * usec );

void dgn_sleepms( int ms );


#ifdef __cplusplus
}
#endif // __cplusplus

#endif // INCLUDED_DGNSC_COMMON_H

